package com.pansky.crm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pansky.crm.common.mybatisplus.ApiResult;
import com.pansky.crm.common.mybatisplus.QueryWapperUtils;
import com.pansky.crm.common.mybatisplus.ResultPageDto;
import com.pansky.crm.common.mybatisplus.SuperServiceImpl;
import com.pansky.crm.entity.UserLoginLog;
import com.pansky.crm.entity.UserOperateLog;
import com.pansky.crm.entity.bo.page.UserLoginLogPageBo;
import com.pansky.crm.mapper.mysql.UserLoginLogMapper;
import com.pansky.crm.service.UserLoginLogService;
import com.pansky.crm.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

/**
 * @author ：zhouzhiyong
 * @date ：Created in 2021/3/2 9:16
 * @description：登录日志服务
 */
@Service
@DS("mysqlDb")
public class UserLoginLogServiceImpl extends SuperServiceImpl<UserLoginLogMapper, UserLoginLog> implements UserLoginLogService {

    /**
     * @author yandanyang
     * @description 分页查询
     * @date 2019-05-15 11:32:14
     */
    public ApiResult<ResultPageDto> queryByPage(UserLoginLogPageBo queryDTO) {
        IPage<UserLoginLog> page = new Page<>(queryDTO.getPage(), queryDTO.getLimit());
        QueryWrapper<UserLoginLog> queryWrapper = QueryWapperUtils.queryToWrapper(queryDTO,UserLoginLog.class);
        queryWrapper.orderByDesc("id");
        IPage<UserLoginLog> dbResult = page(page, queryWrapper);
        //业务数据
        ResultPageDto resultPageDto = defaultTransformPageData(dbResult,
                transform -> dbResult.getRecords().stream().map(userLoginLog -> {
                    UserLoginLog logDto = new UserLoginLog();
                    BeanUtil.copyProperties(userLoginLog, logDto);
                    return logDto;
                }).collect(Collectors.toList()));
        return ApiResult.ok(resultPageDto);

    }
}
